package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.TestTypeDetailsMapper;
import com.yxuan.springboot.mapper.TestVersionTopicMapper;
import com.yxuan.springboot.model.TestTypeDetails;
import com.yxuan.springboot.model.TestVersionTopic;
import com.yxuan.springboot.service.TestVersionTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author zchao
 *    TestVersionTopic业务层实现类
 * @date 2023-05-16 14:22:57
 */
@Service
@Transactional
public class TestVersionTopicServiceImpl extends BaseServiceImpl<TestVersionTopic> implements TestVersionTopicService {

    @Autowired
    private TestVersionTopicMapper testVersionTopicMapper;

    @Autowired
    private TestTypeDetailsMapper testTypeDetailsMapper;

//    添加题目，添加一个题目，版本的题目数+1
    @Override
    public HashMap<String, Object> addOptionsToTopic(Map map) throws Exception {
        List<Integer> list = (List) map.get("list");
        HashMap<String,Object> result = new HashMap<>();
        TestTypeDetails testTypeDetails = new TestTypeDetails();
        testTypeDetails.setId((Integer) map.get("type_id"));
        testTypeDetails.setVersion((String) map.get("version"));
        TestTypeDetails testTypeDetails1 = testTypeDetailsMapper.queryObjectByPramas(testTypeDetails);
        for (Integer i : list){
            TestVersionTopic testVersionTopic = new TestVersionTopic();
            testVersionTopic.setVersion((String) map.get("version"));
            testVersionTopic.setTopicId(i);
            testVersionTopic.setTypeId((Integer) map.get("type_id"));
            testVersionTopicMapper.insert(testVersionTopic);
//            版本的题目数+1
            testTypeDetails1.setTopicNum(testTypeDetails1.getTopicNum()+1);
            testTypeDetailsMapper.update(testTypeDetails1);
        }
        result.put("add","success");
        return result;
    }

    @Override
    public HashMap<String, Object> delOptionsToTopic(Map map) throws Exception {
        List<Integer> list = (List) map.get("list");
        HashMap<String,Object> result = new HashMap<>();
        TestTypeDetails testTypeDetails = new TestTypeDetails();
        testTypeDetails.setId((Integer) map.get("type_id"));
        testTypeDetails.setVersion((String) map.get("version"));
        for (Integer i : list){
            TestVersionTopic testVersionTopic = new TestVersionTopic();
            testVersionTopic.setVersion((String) map.get("version"));
            testVersionTopic.setTopicId(i);
            testVersionTopic.setTypeId((Integer) map.get("type_id"));
            testVersionTopicMapper.delete(testVersionTopic);
//        删除一个题目，版本的题目数-1
            TestTypeDetails testTypeDetails1 = testTypeDetailsMapper.queryObjectByPramas(testTypeDetails);
            testTypeDetails1.setTopicNum(testTypeDetails1.getTopicNum()-1);
            testTypeDetailsMapper.update(testTypeDetails1);
        }
        result.put("del","success");
        return result;
    }
}
